package com.jiawa.wiki.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jiawa.wiki.domain.User;
import com.jiawa.wiki.domain.UserExample;
import com.jiawa.wiki.exception.BusinessException;
import com.jiawa.wiki.exception.BusinessExceptionCode;
import com.jiawa.wiki.mapper.UserMapper;
import com.jiawa.wiki.req.UserLoginReq;
import com.jiawa.wiki.req.UserQueryReq;
import com.jiawa.wiki.req.UserSaveReq;
import com.jiawa.wiki.resp.PageResp;
import com.jiawa.wiki.resp.UserLoginResp;
import com.jiawa.wiki.resp.UserQueryResp;
import com.jiawa.wiki.utill.CopyUtil;
import com.jiawa.wiki.utill.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;



@Service
public class UserService {
    private final static Logger LOG = LoggerFactory.getLogger(UserService.class);
    @Resource
    UserMapper userMapper;
    @Resource
    SnowFlake snowFlake;

    public PageResp<UserQueryResp> list(UserQueryReq req){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        if (!ObjectUtils.isEmpty(req.getLoginName())) {
            criteria.andLoginNameEqualTo(req.getLoginName());
        }
        List<User> users = userMapper.selectByExample(userExample);
        PageHelper.startPage(req.getPage(), req.getSize());
        PageInfo<User> userPageInfo = new PageInfo<>(users);
        List<UserQueryResp> userQueryRespList = CopyUtil.copyList(users, UserQueryResp.class);
        PageResp<UserQueryResp> pageResp = new PageResp<>();
        pageResp.setList(userQueryRespList);
        pageResp.setTotal(userPageInfo.getTotal());
        return pageResp;
    }
    public void save(UserSaveReq req){
        User user = CopyUtil.copy(req,User.class);
        if (ObjectUtils.isEmpty(req.getId())){
            if ( ObjectUtils.isEmpty(selectByUserName(req.getLoginName())) ){
                user.setId(snowFlake.nextId());
                userMapper.insert(user);
            }else {
                throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
            }
        }else {
            //将登录名设置为空
            req.setLoginName(null);
            req.setPassword(null);
            //这个方法的意思是 user 属性有值我才去更新 反之不会
            userMapper.updateByPrimaryKeySelective(user);
        }

    }
    public void delete(Long id){
        userMapper.deleteByPrimaryKey(id);
    }
    public User selectByUserName(String  loginName){
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andNameEqualTo(loginName);
        List<User> users = userMapper.selectByExample(userExample);
        if(CollectionUtils.isEmpty(users)){
            return null;
        }else {
           return users.get(0);
        }

    }

    public void resetPassword(UserSaveReq req){
        User user = CopyUtil.copy(req,User.class);
        userMapper.updateByPrimaryKeySelective(user);
    }
    public UserLoginResp login(UserLoginReq req){
        User userDb = CopyUtil.copy(req,User.class);
        //先判断用户名
        if(ObjectUtils.isEmpty(req.getLoginName())){
            LOG.info("登录名不存在名错误{}" ,req.getLoginName());
            throw new BusinessException(BusinessExceptionCode.USER_LOGIN_NAME_EXIST);
        }else {
            if(userDb.getPassword().equals(req.getPassword())){
                UserLoginResp resp = CopyUtil.copy(userDb, UserLoginResp.class);
                return resp;
            }else {
                LOG.info("用户密码错误{}",req.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_USER_ERROR);
            }
        }


    }


}
